package com.d4a5.mapping.google2
{
	import com.d4a5.mapping.core.AmfFactory;
	import com.d4a5.mapping.core.AmfObject;
	import com.google.maps.LatLng;
	import com.google.maps.overlays.MarkerOptions;
	import com.google.maps.styles.FillStyle;
	import com.google.maps.styles.GradientStyle;
	import com.google.maps.styles.StrokeStyle;
	
	import flash.geom.Matrix;
	
	public class wmgOverlayLayerMarker extends wmgOverlayLayerBaseShape
	{
		public function wmgOverlayLayerMarker(geometry:Object,options:MarkerOptions)
		{
			var amf:AmfObject;
			if( geometry is AmfObject ){
				amf = geometry as AmfObject;
			}
			else if( geometry is LatLng ){
				amf = AmfFactory.Create( AmfFactory.formatGoogle , AmfFactory.typePoint , geometry , null );
			}
			super(amf);
	
			this.setOptions( (options) ? MarkerOptions.merge( [options,MarkerOptions.getDefaultOptions()] ) : MarkerOptions.getDefaultOptions() );
		}
		
		override protected function __redraw(m:Matrix):void {
			this.graphics.clear();
			this._drawn = false;
			
			var options:MarkerOptions = this.getOptions() as MarkerOptions;
			var stroke:StrokeStyle = options.strokeStyle;
			var fill:FillStyle = options.fillStyle;
			var gFill:GradientStyle;
			if( fill ){
				gFill = options.fillStyle.gradient;
			}
			//this.toolTip = options.tooltip;
			
			if( options.radius > 0 ){
				var drawOutline:Boolean = false;
				if( stroke && stroke.alpha > 0 && stroke.thickness > 0 ){
					this.graphics.lineStyle( stroke.thickness as Number, stroke.color as uint , stroke.alpha as Number , stroke.pixelHinting );
					drawOutline = true;
				}
				
				if( gFill ){
					this.graphics.beginGradientFill( gFill.type , gFill.colors , gFill.alphas , gFill.ratios , gFill.matrix , gFill.spreadMethod , gFill.interpolationMethod , gFill.focalPointRatio );
					amf.drawCircleOnGraphicsNoTranslation( this.graphics , m , options.radius as Number );
					this.graphics.endFill();
					this._drawn = true;
				}
				else if( fill ){
					this.graphics.beginFill( fill.color as uint , fill.alpha as Number );
					amf.drawCircleOnGraphicsNoTranslation( this.graphics , m , options.radius as Number );
					this.graphics.endFill();
					this._drawn = true;
				}
				else if( drawOutline ){
					amf.drawCircleOnGraphicsNoTranslation( this.graphics , m , options.radius as Number );
					this._drawn = true;
				}
			}
		}
		
		override public function setOptions(o:Object):void {
			if( o is MarkerOptions && this._options != o ){
				this._options = o;
				if( this._isAdded ){
					this.redraw();
				}
			}
		}
		
		override public function getDefaultOptions() : Object {
			return MarkerOptions.getDefaultOptions();
		}
		override public function mergeOptions( toBeMerged:Array ) : Object {
			return MarkerOptions.merge( toBeMerged );
		}
		
		override public function updateGeometry( geometry:Object ) : void { 
			var amf:AmfObject;
			if( geometry is AmfObject ){
				amf = geometry as AmfObject;
			}
			else if( geometry is LatLng ){
				amf = AmfFactory.Create( AmfFactory.formatGoogle , AmfFactory.typePoint , geometry , null );
			}
			
			this.updateAmf( amf );
		}
	}
}